package com.cinc.ebc.mm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cinc.ebc.mm.entity.MmMaterialPicking;
import com.cinc.ebc.mm.entity.MmMaterialPickingDetail;
import com.cinc.ebc.mm.entity.MmMaterialStock;
import com.cinc.ebc.mm.mapper.MmMaterialPickingDetailMapper;
import com.cinc.ebc.mm.mapper.MmMaterialPickingMapper;
import com.cinc.ebc.mm.mapper.MmMaterialStockMapper;
import com.cinc.ebc.mm.model.MmMaterialPickingStockModel;
import com.cinc.ebc.mm.service.IMmMaterialPickingDetailService;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 材料领用明细
 * @Author: jeecg-boot
 * @Date:   2023-04-16
 * @Version: V1.0
 */
@Service
public class MmMaterialPickingDetailServiceImpl extends ServiceImpl<MmMaterialPickingDetailMapper, MmMaterialPickingDetail> implements IMmMaterialPickingDetailService {

	@Autowired
	private MmMaterialPickingDetailMapper mmMaterialPickingDetailMapper;

	@Autowired
	private MmMaterialStockMapper mmMaterialStockMapper;

	@Autowired
	private MmMaterialPickingMapper mmMaterialPickingMapper;


	@Override
	public List<MmMaterialPickingDetail> selectByMainId(String mainId) {
		return mmMaterialPickingDetailMapper.selectByMainId(mainId);
	}

	@Override
	public List<MmMaterialPickingStockModel> queryMaterialPickingStockList(String mainId) {
		List<MmMaterialPickingStockModel> pickingStockModelList= new ArrayList<>();
		// 获取领料明细
		List<MmMaterialPickingDetail> materialPickingDetailList = mmMaterialPickingDetailMapper.selectByMainId(mainId);
		materialPickingDetailList = materialPickingDetailList.stream()
				.filter(pickingDetail -> pickingDetail.getOutQty().compareTo(pickingDetail.getQty()) == -1).collect(Collectors.toList());
		if(oConvertUtils.listIsEmpty(materialPickingDetailList)){
			return new ArrayList<>();
		}
		// 查询领料明细及对应库存信息
		LambdaQueryWrapper<MmMaterialStock> materialStockQuery = new LambdaQueryWrapper<>();
		List<String> materialIdList = materialPickingDetailList.stream().map(MmMaterialPickingDetail::getMaterialId).collect(Collectors.toList());
		materialStockQuery.in(MmMaterialStock::getMaterialId, materialIdList);
		materialStockQuery.gt(MmMaterialStock::getStockQtyAvailable, BigDecimal.ZERO);
		MmMaterialPicking mmMaterialPicking = mmMaterialPickingMapper.selectById(mainId);
		if(oConvertUtils.isNotEmpty(mmMaterialPicking.getProjectId())){
			// 查本项目或无项目的库存
			materialStockQuery.and(query -> query.eq(MmMaterialStock::getProjectId,"").or().eq(MmMaterialStock::getProjectId, mmMaterialPicking.getProjectId()));
		}
		List<MmMaterialStock> materialStockList = mmMaterialStockMapper.selectList(materialStockQuery);
		if(oConvertUtils.listIsEmpty(materialStockList)){
			return new ArrayList<>();
		}
		for(MmMaterialPickingDetail materialPickingDetail : materialPickingDetailList){
			List<MmMaterialStock> detailStockList = materialStockList.stream()
					.filter(stock -> stock.getMaterialId().equals(materialPickingDetail.getMaterialId())).collect(Collectors.toList());
			if(oConvertUtils.listIsEmpty(detailStockList)){
				// 无可用库存
				MmMaterialPickingStockModel pickingStockModel = new MmMaterialPickingStockModel();
				pickingStockModel.setId(materialPickingDetail.getId());
				pickingStockModel.setDetailId(materialPickingDetail.getId());
				pickingStockModel.setMainId(mainId);
				pickingStockModel.setLineNo(materialPickingDetail.getLineNo());
				pickingStockModel.setQty(BigDecimal.ZERO);
				pickingStockModel.setBillQty(materialPickingDetail.getQty());
				pickingStockModel.setPickingQty(materialPickingDetail.getOutQty());
				pickingStockModel.setUnpickingQty(materialPickingDetail.getQty().subtract(materialPickingDetail.getOutQty()));
				pickingStockModel.setMaterialId(materialPickingDetail.getMaterialId());
				pickingStockModel.setMaterialNo(materialPickingDetail.getMaterialNo());
				pickingStockModel.setMaterialDesc(materialPickingDetail.getMaterialDesc());
				pickingStockModel.setUnit(materialPickingDetail.getUnit());
				pickingStockModel.setStockQty(BigDecimal.ZERO);
				pickingStockModel.setUnitPrice(BigDecimal.ZERO);
				pickingStockModel.setSumPrice(BigDecimal.ZERO);
				pickingStockModel.setBatchNo("");
				pickingStockModel.setWarehouseId("");
				pickingStockModel.setStorageLocationId("");
				pickingStockModel.setProjectId("");
				pickingStockModel.setStockQtyAllocate(BigDecimal.ZERO);
				pickingStockModel.setStockQtyAvailable(BigDecimal.ZERO);

				pickingStockModelList.add(pickingStockModel);
			}else{
				// 有可用库存
				for(MmMaterialStock materialStock : detailStockList){
					MmMaterialPickingStockModel pickingStockModel = new MmMaterialPickingStockModel();
					pickingStockModel.setId(materialPickingDetail.getId()+materialStock.getId());
					pickingStockModel.setDetailId(materialPickingDetail.getId());
					pickingStockModel.setMainId(mainId);
					pickingStockModel.setLineNo(materialPickingDetail.getLineNo());
					pickingStockModel.setQty(BigDecimal.ZERO);
					pickingStockModel.setBillQty(materialPickingDetail.getQty());
					pickingStockModel.setPickingQty(materialPickingDetail.getOutQty());
					pickingStockModel.setUnpickingQty(materialPickingDetail.getQty().subtract(materialPickingDetail.getOutQty()));
					pickingStockModel.setMaterialId(materialPickingDetail.getMaterialId());
					pickingStockModel.setMaterialNo(materialPickingDetail.getMaterialNo());
					pickingStockModel.setMaterialDesc(materialPickingDetail.getMaterialDesc());
					pickingStockModel.setUnit(materialPickingDetail.getUnit());
					pickingStockModel.setStockQty(materialStock.getStockQty());
					pickingStockModel.setUnitPrice(materialStock.getUnitPrice());
					pickingStockModel.setSumPrice(materialStock.getSumPrice());
					pickingStockModel.setBatchNo(materialStock.getBatchNo());
					pickingStockModel.setWarehouseId(materialStock.getWarehouseId());
					pickingStockModel.setStorageLocationId(materialStock.getStorageLocationId());
					pickingStockModel.setProjectId(materialStock.getProjectId());
					pickingStockModel.setStockQtyAllocate(materialStock.getStockQtyAllocate());
					pickingStockModel.setStockQtyAvailable(materialStock.getStockQtyAvailable());

					pickingStockModelList.add(pickingStockModel);
				}
			}
		}

		return pickingStockModelList;
	}
}
